/*
 * Copyright (c) 2005 - 2007 metadude.com, All rights reserved.
 * Email: community@polarion.org
 *
 * This program and the accompanying materials are made available under the
 * terms of the GNU General Public License 2.0 (the "License"). You may not use
 * this file except in compliance with the License. Copy of the License is
 * located in the file LICENSE.txt in the project distribution. You may also
 * obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 *
 *
 * METADUDE.COM MAKES NO REPRESENTATIONS OR WARRANTIES
 * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. POLARION SOFTWARE
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */
using mshtml;

namespace poba.Core.Html {
    public class Element {
        private IHTMLElement _element;
        private Style _style;

        public string ClassName {
            get { return _element.className; }
            set { _element.className = value; }
        }

        public IHTMLElement HtmlElement {
            get { return _element; }
        }

        public string ID {
            get { return _element.id; }
            set { _element.id = value; }
        }

        public string InnerHTML {
            get { return _element.innerHTML; }
            set { _element.innerHTML = value; }
        }

        public string InnerText {
            get { return _element.innerText; }
            set { _element.innerText = value; }
        }

        public int OffsetHeight {
            get { return _element.offsetHeight; }
        }

        public int OffsetLeft {
            get { return _element.offsetLeft; }
        }

        public Element OffsetParent {
            get { return CreateMostSpecificType(_element.offsetParent); }
        }

        public int OffsetTop {
            get { return _element.offsetTop; }
        }

        public int OffsetWidth {
            get { return _element.offsetHeight; }
        }

        public string OuterHTML {
            get { return _element.outerHTML; }
            set { _element.outerHTML = value; }
        }

        public string OuterText {
            get { return _element.outerText; }
            set { _element.outerText = value; }
        }

        public Element ParentElement {
            get { return CreateMostSpecificType(_element.parentElement); }
        }

        public Style Style {
            get { return _style; }
        }

        public string TagName {
            get { return _element.tagName; }
        }

        public static Element CreateMostSpecificType(IHTMLElement element) {
            switch(element.tagName) {
                case "A" :
                    return new AnchorElement(element);
                case "INPUT" :
                    string type = ((IHTMLInputElement) element).type; 
                    switch (type) {
                        case "checkbox" :
                            return new CheckBoxElement(element);
                        case "radio" :
                            return new RadioButtonElement(element);
                        case "text" :
                            return new TextBoxElement(element);
                    }
                    break;
                case "OPTION" :
                    return new OptionElement(element);
                case "SELECT" :
                    return new SelectElement(element);
            }

            return new Element(element);
        }

        public Element(IHTMLElement element) {
            _element = element;
            _style = new Style(_element.style);
        }

        public void Click() {
            _element. click();
        }

        public bool Contains(Element element) {
            return _element.contains(element._element);
        }

        public string GetAttribute(string name) {
            object attribute = _element.getAttribute(name, 0);
            if (attribute is System.DBNull) {
                return "";
            }
            return (string)attribute;
        }

        public void RemoveAttribute(string name) {
            _element.removeAttribute(name, 0);
        }
        
        public void SetAttribute(string name, string value) {
            _element.setAttribute(name, value, 0);
        }
    }
}
